package com.ftg.learnmybatis.learnio;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.List;

/**
 * 学习一下xml解析，之 dom4j
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/3/13 下午3:49
 */
public class Example05 {

    public static void main(String[] args) {
//        dom4j     SAXReader reader = new SAXReader();
//        Document document = reader.read(url);
//        SAXReader 读取 （reader  流读文件）
//                Document      文档
        SAXReader reader = new SAXReader();
        try {
            Document read = reader.read("/home/kangjx/userpanel/workspace/java/learnjava-j2012/src/main/resources/person.xml");

            //根
            Element rootElement = read.getRootElement();
            Persons p = new Persons();
            System.out.println(rootElement.getName() +"根节点名称");
            List<Element> elements = rootElement.elements();
            elements.forEach(x->{
                Person per = new Person(x.attribute("id").getValue(),x.attribute("name").getValue());
                p.getList().add(per);
            });



        } catch (DocumentException e) {
            throw new RuntimeException(e);
        }
    }

}
